package edu.nf.project.webs.storeOperation;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.nf.project.entitys.dto.storeOperation.CustomerOrderDetailsDTO;
import edu.nf.project.entitys.dto.storeOperation.OrderDTO;
import edu.nf.project.entitys.dto.storeOperation.OrderItemDTO;
import edu.nf.project.entitys.dto.storeOperation.COrder_ItemDTO;
import edu.nf.project.entitys.entity.storeOperation.convention.operate.CustomerOrder;
import edu.nf.project.services.exception.derivedAnomalies.CustomerOrderException;
import edu.nf.project.services.storeOperation.CustomerOrderService;
import edu.nf.project.services.response.BaseController;
import edu.nf.project.services.response.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单管理控制器
 */
@RestController
@RequestMapping("/api/orders")
public class CustomerOrderController extends BaseController {
    @Autowired
    private CustomerOrderService customerOrderService;

    /**
     * 创建新订单
     */
    @PostMapping
    public ResultVO<Integer> createOrder(@RequestBody CustomerOrder order) {
        try {
            Integer orderId = customerOrderService.createOrder(order);
            return success(orderId);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            throw new CustomerOrderException(30000, "系统错误，请稍后重试");
        }
    }

    @PostMapping("/insert")
    public ResultVO<Integer> createOrderAndItem(@RequestBody OrderDTO order) {
        Integer orderId = customerOrderService.createOrderAndItem(order);
        System.out.println(orderId);
        return success(orderId);
    }

    /**
     * 获取订单列表
     */
    @GetMapping("/list")
    public ResultVO<Page<CustomerOrder>> getOrderList(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer orderStatus) {
        try {
            Page<CustomerOrder> page = new Page<>(current, size);
            Page<CustomerOrder> result = customerOrderService.getOrderList(page, orderStatus);
            return success(result);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            throw new CustomerOrderException(30001, "获取订单列表失败");
        }
    }

    /**
     * 获取订单详情
     */
    @GetMapping("/{orderId}")
    public ResultVO<CustomerOrder> getOrderInfo(@PathVariable Integer orderId) {
        try {
            CustomerOrder order = customerOrderService.getOrderInfo(orderId);
            return success(order);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            throw new CustomerOrderException(30002, "获取订单详情失败");
        }
    }

    /**
     * 更新订单状态
     */
    @PutMapping("/{orderId}/status")
    public ResultVO<Boolean> updateOrderStatus(
            @PathVariable Integer orderId,
            @RequestParam Integer orderStatus) {
        try {
            boolean result = customerOrderService.updateOrderStatus(orderId, orderStatus);
            return success(result);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            throw new CustomerOrderException(30003, "更新订单状态失败");
        }
    }
    
    /**
     * 获取指定区域的订单列表 (包含餐桌名和操作人名)
     */
    @GetMapping("/region")
    public ResultVO<List<CustomerOrderDetailsDTO>> getOrdersByRegion(@RequestParam(required = false) Integer[] regionIds) {
        try {
            List<CustomerOrderDetailsDTO> orders = customerOrderService.getOrdersByRegion(regionIds);
            return success(orders);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            System.out.println(e);
            throw new CustomerOrderException(30004, "获取区域订单列表失败");
        }
    }

    /**
     * 获取指定订单的菜品列表（包含菜品详细信息）
     */
    @GetMapping("/{orderId}/items")
    public ResultVO<List<OrderItemDTO>> getOrderItems(@PathVariable Integer orderId) {
        try {
            List<OrderItemDTO> items = customerOrderService.getOrderItems(orderId);
            return success(items);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            throw new CustomerOrderException(30005, "获取订单菜品列表失败");
        }
    }

    /**
     * 获取指定日期的全部订单信息（包含订单和菜品信息）
     */
    @GetMapping("/date")
    public ResultVO<List<COrder_ItemDTO>> getOrdersByDate(@RequestParam(required = false) String date) {
        try {
            List<COrder_ItemDTO> orders = customerOrderService.getOrdersByDate(date);
            return success(orders);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            throw new CustomerOrderException(30006, "获取日期订单列表失败");
        }
    }

    @GetMapping("/refund-order/{uId}")
    public ResultVO<List<CustomerOrder>> listCanRefundOrder(@PathVariable Integer uId){
        try {
            List<CustomerOrder> orders = customerOrderService.listCanRefundOrder(uId);
            return success(orders);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            throw new CustomerOrderException(30005, "获取订单菜品列表失败");
        }
    }
}